{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "fdf64be0",
   "metadata": {},
   "source": [
    "This script formats the `txt` predictions from `run_case.py` into a format suitable for Codalab submission."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e6a69aa5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'index': '0', 'prediction': 1},\n",
       " {'index': '1', 'prediction': 1},\n",
       " {'index': '2', 'prediction': 1},\n",
       " {'index': '3', 'prediction': 0},\n",
       " {'index': '4', 'prediction': 1}]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import json\n",
    "import os\n",
    "\n",
    "file_path = r\"D:\\61 Challenges\\2022_CASE_\\CausalNewsCorpus\\CausalNewsCorpus\\outs\\dev\\predict_results_cola.txt\"\n",
    "# label_to_id = {'No':0,'Yes':1}\n",
    "\n",
    "data = []    \n",
    "with open(file_path) as file:\n",
    "    for i,line in enumerate(file):\n",
    "        if i==0:\n",
    "            continue # column name\n",
    "        line = line.rstrip().split('\\t')\n",
    "        data.append({'index':line[0],'prediction':int(line[1])})\n",
    "data[0:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "24710bef",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(f'submission_{os.path.basename(os.path.dirname(file_path))}.json', 'w') as fp:\n",
    "    fp.write('\\n'.join(json.dumps(i) for i in data))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7a908940",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
